/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bgu.ppm.sim.impl.mas;

import bgu.ppm.sim.api.mas.Agent;
import bgu.ppm.sim.api.mas.AgentFactory;
import bgu.ppm.sim.api.mas.MultiAgentSystem;

/**
 *
 * @author User
 */
public class ReflectiveAgentFactory implements AgentFactory {

    private Class<? extends Agent> agentClass;

    public ReflectiveAgentFactory(Class<? extends Agent> agentClass) {
        this.agentClass = agentClass;
    }

    @Override
    public Agent createAgent(int id) {
        try {
            Agent result = agentClass.newInstance();
            result.setId(id);
            return result;
        } catch (InstantiationException | IllegalAccessException ex) {
            throw new ReflectiveAgentCreationFailedException(ex);
        }

    }

    @Override
    public void initialize(MultiAgentSystem system) {
    }

    public static class ReflectiveAgentCreationFailedException extends RuntimeException {

        public ReflectiveAgentCreationFailedException(Throwable cause) {
            super(cause);
        }
    }
}
